package com.huabo.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.monitor.oracle.entity.TblAssess;
import com.huabo.monitor.oracle.mapper.TblAssessMapper;
import com.huabo.monitor.service.TblAssessService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

public class TblAssessServiceImpl implements TblAssessService {

    @Resource
    private TblAssessMapper tblAssessMapper;

    /**
     * 查询-分页展示
     *
     * @param pageNumber
     * @param pageSize
     * @param assNumnber
     * @param assName
     * @param startDate
     * @param startDates
     * @param endDate
     * @param endDates
     * @return
     */
    @Override
    public JsonBean findByPage(Integer pageNumber, Integer pageSize, String assNumnber, String assName, String startDate, String startDates, String endDate, String endDates) {
        if (pageNumber == null || pageNumber <= 0) {
            pageNumber = 1;
        }
        if (pageSize <= 0 || pageSize > 15) {
            pageSize = 15;
        }
        Page<TblAssess> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNumber);

        LambdaQueryWrapper<TblAssess> wrapper = Wrappers.<TblAssess>lambdaQuery();
        if (StringUtils.isNotBlank(assNumnber)) {
            wrapper.eq(TblAssess::getAssessid, assNumnber);
        }
        if (StringUtils.isNotBlank(assName)) {
            wrapper.eq(TblAssess::getAssessname, assName);
        }
        if (StringUtils.isNotBlank(startDate)) {
            wrapper.ge(TblAssess::getStartdate, startDate);
        }
        if (StringUtils.isNotBlank(startDates)) {
            wrapper.le(TblAssess::getStartdate, startDates);
        }
        if (StringUtils.isNotBlank(endDate)) {
            wrapper.ge(TblAssess::getEnddate, endDate);
        }
        if (StringUtils.isNotBlank(endDates)) {
            wrapper.le(TblAssess::getEnddate, endDates);
        }

        Page<TblAssess> tblAssessPage = tblAssessMapper.selectPage(page, wrapper);
        List<TblAssess> assessList = tblAssessPage.getRecords();

        return ResponseFormat.retParam(1, 200, assessList);
    }

    @Override
    public JsonBean findByPage(Integer pageNumber, Integer pageSize, String assNumnber, String assName, String startDate, String endDate) {
        if (pageNumber == null || pageNumber <= 0){
            pageNumber = 1;
        }
        if (pageSize <= 0 || pageSize >15){
            pageSize = 15;
        }
        Page<TblAssess> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNumber);

        LambdaQueryWrapper<TblAssess> wrapper = Wrappers.<TblAssess>lambdaQuery();
        if (StringUtils.isNotBlank(assNumnber)){
            wrapper.eq(TblAssess::getAssessid, assNumnber);
        }
        if (StringUtils.isNotBlank(assName)){
            wrapper.eq(TblAssess::getAssessname, assName);
        }
        if (StringUtils.isNotBlank(startDate)){
            wrapper.ge(TblAssess::getStartdate, startDate);
        }
        if (StringUtils.isNotBlank(endDate)){
            wrapper.le(TblAssess::getEnddate, endDate);
        }


        Page<TblAssess> tblAssessPage = tblAssessMapper.selectPage(page, wrapper);
        List<TblAssess> assessList = tblAssessPage.getRecords();

        return ResponseFormat.retParam(1, 200, assessList);
    }


    /**
     * 项目立项 -- 新建
     *
     * @return
     */
    @Override
    public JsonBean add(TblAssess tblAssess) {
        tblAssess.setAssstatus("1");//状态 1 创建、2 启动

        tblAssessMapper.insert(tblAssess);

        return ResponseFormat.retParam(1, 200, "success");
    }

    /**
     * 评价项目 -- 修改
     *
     * @param tblAssess
     * @return
     */
    @Override
    public JsonBean update(TblAssess tblAssess) {
        if (null == tblAssess) {
            return ResponseFormat.retParam(0, 10002, "缺少参数，请确认后重试");
        }
        TblAssess dbTblAssess = tblAssessMapper.selectById(tblAssess.getAssid());
        if (null == dbTblAssess) {
            return ResponseFormat.retParam(0, 50002, "修改的数据不存在，请确认后重试");
        }
        tblAssessMapper.updateById(tblAssess);

        return ResponseFormat.retParam(1, 200, "success");
    }

    /**
     * 项目立项 -- 删除
     *
     * @param assId
     * @return
     */
    @Override
    public JsonBean delete(BigDecimal assId) {

        TblAssess tblAssess = tblAssessMapper.selectById(assId);
        if (null == tblAssess) {
            return ResponseFormat.retParam(0, 50001, "删除数据已不存在，请确认后重试");
        }
        if (!"1".equals(tblAssess.getAssstatus())) {
            return ResponseFormat.retParam(0, 1000, "项目状态不能删除");
        }

        tblAssessMapper.deleteById(assId);

        return ResponseFormat.retParam(1, 200, "success");
    }

}
